﻿using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.Services
{
    public partial class ServiceList : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var filter = new ServiceFilter();
                ViewState["filter"] = filter;
                ViewState["sortingButton"] = LinkButtonAll.UniqueID;
            }
        }

        /// <summary>
        /// Handles the Selecting event of the ObjectDataSourceGor control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void ObjectDataSourceServiceList_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as ServiceFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Handles the Command event of the FilterByLetters control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            var filter = ViewState["filter"] as ServiceFilter;
            if (filter != null)
            {
                Collection<string> beginningLetters = StringTools.GetLetterGroup(e.CommandName);
                filter.SetBeginningLetters(beginningLetters);

                var letterGroupId = ViewState["sortingButton"] as string;
                var letterGroupButton = FindControl(letterGroupId) as LinkButton;
                if (letterGroupButton != null)
                {
                    var clickedButton = sender as LinkButton;
                    if (clickedButton == null)
                    {
                        return;
                    }

                    letterGroupButton.CssClass = string.Empty;
                    clickedButton.CssClass = "hg-blue";
                    ViewState["sortingButton"] = clickedButton.UniqueID;
                }

                GridViewServiceList.PageIndex = 0;
                GridViewServiceList.DataBind();
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as ServiceFilter;
            if (filter != null) filter.IncludeInactive = CheckBoxInactive.Checked;
            GridViewServiceList.DataBind();
        }

        protected void ButtonCopy_Click(object sender, EventArgs e)
        {
            var selectedServiceId = Request.Form["RadioButton"];
            if (selectedServiceId != null)
            {
                Response.Redirect("~/Services/ServiceDetails.aspx?copyId=" + selectedServiceId);
            }

            Response.Write("<script>alert('Please select a service to copy')</script>");
        }

        protected void LinkButtonServiceName_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("False"))
            {
                var serviceId = int.Parse(e.CommandName, CultureInfo.CurrentCulture);
                ServiceBO.SetServiceStatus(serviceId, true);
            }

            Response.Redirect("~/Services/ServiceDetails.aspx?id=" + e.CommandName);
        }
    }
}